#include "weldendinstructionview.h"
#include "ui_weldendinstructionview.h"

WeldEndInstructionView::WeldEndInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WeldEndInstructionView)
{
    ui->setupUi(this);
    setGeometry(5,50,451,544);
    initialDisplayStyle(styleId);
    ui->radioButton_conditionIndex->setChecked(true);
    ui->radioButton_conditionValue->setChecked(false);
}

void WeldEndInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}


void WeldEndInstructionView::getInput(WeldEndInstruction &instructionParaOut)
{
    instructionParaOut.comment=ui->note_lineEdit->text();
    instructionParaOut.weldPropertyIndex=ui->comboBox_weldProperty->currentIndex();
    if(ui->radioButton_conditionIndex->isChecked())
    {
        instructionParaOut.weldConditionIndex=ui->comboBox_conditionIndex->currentIndex();
        instructionParaOut.weldType=E_WELDEND_TYPE_COMMON;
    }
    else {
        instructionParaOut.weldVoltage=ui->lineEdit_weldVoltage->text().toInt();
        instructionParaOut.weldAmp=ui->lineEdit_weldCurrent->text().toInt();
        instructionParaOut.weldTime=ui->lineEdit_weldTime->text().toDouble();
        instructionParaOut.weldType=E_WELDEND_TYPE_AMP;
    }

}


void WeldEndInstructionView::updateView()
{

    ui->comboBox_weldProperty->setCurrentIndex(instructionPara.weldPropertyIndex);
    switch (instructionPara.weldType) {
    case E_WELDEND_TYPE_COMMON:
    ui->comboBox_conditionIndex->setCurrentIndex(instructionPara.weldConditionIndex);
    ui->radioButton_conditionIndex->setChecked(true);
    ui->radioButton_conditionValue->setChecked(false);
    break;
    case E_WELDEND_TYPE_AMP:
    ui->radioButton_conditionIndex->setChecked(false);
    ui->radioButton_conditionValue->setChecked(true);
    ui->lineEdit_weldVoltage->setText(QString::number(instructionPara.weldVoltage));
    ui->lineEdit_weldCurrent->setText(QString::number(instructionPara.weldAmp));
    ui->lineEdit_weldTime->setText(QString::number(instructionPara.weldTime,10,2));
    break;

    default:
        break;
    }
    ui->note_lineEdit->setText(instructionPara.comment);
}

void WeldEndInstructionView::changeLanguage(int language)
{
   //TODO
}


void WeldEndInstructionView::finishEditInstruction(WeldEndInstruction instructionParaIn,QString &instructionStrOut)
{
    instructionStrOut.clear();
    instructionStrOut="Weld End[";
    instructionStrOut+=QString::number(instructionParaIn.weldPropertyIndex)+",";
    switch (instructionParaIn.weldType) {
    case E_WELDEND_TYPE_COMMON:
        instructionStrOut+=QString::number(instructionParaIn.weldConditionIndex)+"]";
        break;
    case E_WELDEND_TYPE_AMP:
        instructionStrOut+=QString::number(instructionParaIn.weldVoltage)+"V,"
                +QString::number(instructionParaIn.weldAmp)+"A,"
                +QString::number(instructionParaIn.weldTime,10,2)+"sec]";
        break;
    default:
        break;
    }
    instructionStrOut+=";//"+instructionParaIn.comment;

}





WeldEndInstructionView::~WeldEndInstructionView()
{
    delete ui;
}

void WeldEndInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->setText("");
}

void WeldEndInstructionView::on_save_Btn_clicked()
{

    QString str1 = ui->lineEdit_weldVoltage->text();
    QString str2 = ui->lineEdit_weldTime->text();
    QString str3 = ui->lineEdit_weldCurrent->text();

    QString pattern("^[0-9]+(.[0-9]{1,5})?$");
    QString pattern1("^[0-9]+$");
    QRegExp rxRoller(pattern);
    QRegExp rxRoller1(pattern1);

    if(ui->radioButton_conditionValue->isChecked())
    {
    if(!rxRoller.exactMatch(str2))
    {
       emit showErrorMsgBox(QString("存在非法字符，请修改！"));
       return;
    }
    else if(!rxRoller1.exactMatch(str1)||!rxRoller1.exactMatch(str3) )
    {
        emit showErrorMsgBox(QString("存在非法字符，请修改！"));
        return;
    }
    else
    {
    getInput(instructionPara);
    finishEditInstruction(instructionPara,instructionStr);
//    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionStr);
    }

    }
    else
    {
    getInput(instructionPara);
    finishEditInstruction(instructionPara,instructionStr);
//    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionStr);
    }
}


void WeldEndInstructionView::on_button_editCondition_clicked()
{
    emit showConditionView();
}

void WeldEndInstructionView::on_button_editProperty_clicked()
{
     emit showPropertyView();
}

void WeldEndInstructionView::on_radioButton_conditionIndex_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_conditionIndex->setChecked(true);
        ui->radioButton_conditionValue->setChecked(false);
    }
    else
    {

    }
}

void WeldEndInstructionView::on_radioButton_conditionValue_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_conditionValue->setChecked(true);
        ui->radioButton_conditionIndex->setChecked(false);
    }
    else
    {

    }
}
